﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WgFrame.Utils;
using WgFrame.Net;

public class Hero
{
    /// <summary>
    /// 唯一id
    /// </summary>
    public long id { get; set; }

    /// <summary>
    /// 上阵位置
    /// </summary>
    public byte szPos { get; set; }

    /// <summary>
    /// 布阵位置
    /// </summary>
    public byte bzPos { get; set; }

    /// <summary>
    /// 数据表id
    /// </summary>
    public int sId { get; set; }

    /// <summary>
    /// 渡劫等级
    /// </summary>
    public short DuJieLevel { get; set; }

    /// <summary>
    /// 进阶等级
    /// </summary>
    public short JinJieLevel { get; set; }

    /// <summary>
    /// 星星等级
    /// </summary>
    public short StarLevel { get; set; }

    /// <summary>
    /// 飞升等级
    /// </summary>
    public short FeiShengLevel { get; set; }

    /// <summary>
    /// 属性
    /// </summary>
    public Attribute Attribute { get; private set; }

    /// <summary>
    /// 静态数据
    /// </summary>
    public InitHero InitHero { get; private set; }

    public Hero()
    {
        Attribute = new Attribute();
        DuJieLevel = 1;
    }

    public Hero(int sId) : this()
    {
        this.sId = sId;
        if (InitHeroMgr.Dict.TryGetValue(this.sId, out InitHero initRole))
        {
            InitHero = initRole;
        }
        CountAttribute();
    }

    public Hero(Hashtable data) : this()
    {
        LoadFromHashtable(data);
        if (InitHeroMgr.Dict.TryGetValue(sId, out InitHero initRole))
        {
            InitHero = initRole;
        }
        CountAttribute();
    }

    public bool IsPlayer()
    {
        return sId == 1;
    }

    public string GetName()
    {
        if (IsPlayer())
        {
            return GameNetwork.Player.Name;
        }
        else
        {
            return InitHero == null ? "null" : InitHero.name;
        }
    }

    private void LoadFromHashtable(Hashtable data)
    {
        if (data.ContainsKey((byte)1))
        {
            id = (long)data[(byte)1];
        }

        if (data.ContainsKey((byte)2))
        {
            sId = (int)data[(byte)2];
        }

        if (data.ContainsKey((byte)3))
        {
            DuJieLevel = (short)data[(byte)3];
        }

        if (data.ContainsKey((byte)4))
        {
            JinJieLevel = (short)data[(byte)4];
        }

        if (data.ContainsKey((byte)5))
        {
            StarLevel = (short)data[(byte)5];
        }

        if (data.ContainsKey((byte)6))
        {
            FeiShengLevel = (short)data[(byte)6];
        }
        szPos = (byte)data[(byte)7];
        bzPos = (byte)data[(byte)8];
    }

    private void CountAttribute()
    {
        if (InitHero == null)
        {
            return;
        }


        Attribute.Add(AttributeType.HP, InitHero.hp);
        Attribute.Add(AttributeType.AMP, InitHero.amp);
        Attribute.Add(AttributeType.ATK, InitHero.atk);
        Attribute.Add(AttributeType.CRT, InitHero.crt);
        Attribute.Add(AttributeType.DamageRes, InitHero.damage_res);
        Attribute.Add(AttributeType.DEF, InitHero.def);
        Attribute.Add(AttributeType.DEX, InitHero.dex);
        Attribute.Add(AttributeType.Energy, InitHero.energy);
        Attribute.Add(AttributeType.EVD, InitHero.evd);
        Attribute.Add(AttributeType.KBL, InitHero.kbl);
        Attribute.Add(AttributeType.RGS, InitHero.rgs);
        Attribute.Add(AttributeType.SkillRes, InitHero.skill_res);
        Attribute.Add(AttributeType.SkillUp, InitHero.skill_up);
    }
}
